<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>

      <!-- table -->
      <!-- 按车型新增 -->
      <FormPanel ref="formPanel" :list="formList1" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false"  @formChange="formChangeHandle"/>

      <div style="padding-top: 1rem;"></div>
      <div
        :style="{
          position: 'absolute',
          left: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right'
        }"
      >
        <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
    <BaseDialog :visible.sync="ModelVisible" :title="'车型选择'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <ModelVehicle @close="modelClose" @save="modelSave"></ModelVehicle>
    </BaseDialog>
    </Spin>
  </div>

</template>
<script>
import { dictionary } from '@/utils/dictMixin';//字典的引入
import { saveOne } from '@/api/claim/factory/guaranteeOil';


import ModelVehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
  export default {
    mixins: [dictionary],
    components: {
      ModelVehicle
    },
    data() {
      return {

        dataList1: [],
        visible: false,
        visibleDrawer: false,
        collapse: false,
        visibleAdd:false,
        loading: false,
        ModelVisible: false,
        fetchParams: {}, //ipa 接口
        selectedRows: [], //选中经销商
        formList1: this.createFormList1(),
        saveData: null,

      };

    },
     created() {

     },
    methods: {
      async multiple(val) {
      this.formList.find((x) => x.fieldName == 'mngLab').initialValue = parseFloat((val/ this.para1)*100).toFixed(2);
      // const res = await multiples({ A: this.para1, B: val });
      // if (res.resultCode == 200) {
      //   this.formList.find((x) => x.fieldName == 'labPrice').initialValue = res.data;
      // }
    },

      formChangeHandle(val) {
        console.log(val);
        this.saveData = val;
      },
      //搜索获取数据
      changeHandle(val) {
        this.fetchParams = val;
        console.log(val);
      },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
      // //保存
      // chooseModelName() {
      //   this.visibleDrawer = true;
      // },
      closeAdd() {
        this.visibleDrawer = false;
      },
      save(obj) {
        this.formList1.find(x => x.fieldName == 'modelName').initialValue = obj[0].modelName;
        this.formList1.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
        //this.modelName = obj[0].modelName;
        this.visibleDrawer = false;
      },
      saveAdd() {
        this.visibleDrawer = false;
        this.visibleAdd = false;
        this.fetchParams = { ...this.fetchParams };
      },
      //新增保存
      async saveDialog() {
        this.$refs.formPanel.SUBMIT_FORM();
        if (this.saveData !== null) {
          this.saveData.modelId = this.modelId;
          this.loading = true;
          let res = await saveOne(this.saveData);
          console.log(res);
          if (res.resultCode === 200) {
            this.$emit('save');
            return this.$notify.success({ title: '提示信息', message: '保存成功' });
          } else {
            this.loading = false;
            // this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
            // this.closeDialog();
          }
        }
      },
      //取消操作
      closeDialog() {
        this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
      },
      modelTurnOn() {
      this.ModelVisible = true;
      },
      modelClose(){
      this.ModelVisible = false;
      },

      modelSave(obj) {
      this.formList1.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
      this.formList1.find(x => x.fieldName == 'modelName').initialValue = obj[0].modelName;
      this.modelId = obj[0].modelId;
      this.ModelVisible = false;
    },
      createFormList1() {
        return [
            {
            type: 'INPUT',
            label: '车型编码',
            fieldName: 'modelCode',
            placeholder: '请选择车型编码...',
            readonly: true,// 只读
            rules: [{required: true, message: '请选择车型代码...', trigger: 'change'}],
            unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.modelTurnOn()}/>
              </div>
            );
           }
          },
        {
          type: 'INPUT',
          label: '车型名称',
          fieldName: 'modelName',
          readonly: true,// 只读
          disabled: true, // 置灰
          placeholder: '请选择车型名称...',
          rules: [{required: true, message: '请输入车型名称...', trigger: 'change'}],
        },

          {
            type: 'SELECT',
            label: '类型',
            fieldName: 'type',
            itemList: [],
            placeholder: '请输入类型...',
            filterable: true,
            rules: [{ required: true, message: '请选择类型', trigger: 'change' }],
            itemList: this.createDictList('5593'),
            editRequired: true
          },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'ptStatus',
          filterable: true,
          placeholder: '请选择状态...',
          initialValue: '1',
          itemList: [{text: '启用', value: '1'}, {text: '禁用', value: '0'}],
          rules: [{required: true, message: '请选择状态...', trigger: 'change'}],
        },
          {
          type: 'INPUT',
          label: '工时单价',
          fieldName: 'labPrice',
          placeholder: '请输入工时单价',
          // disabled: true,
          unitRender: (p) => {
            return <div>元</div>;
          },
          rules: [
            { required: true, message: '请输入工时单价', trigger: 'change' },
            {
              validator: (rule, value, callback) => {
                let reg = /^(\d+)(\.?)(\d{0,2})$/;
                if (value < 0) {
                  return callback(new Error('工时单价,最小为0'));
                }
                if(!reg.test(Number(value))){
                  return callback(new Error('工时单价,最多2位小数'));
                }
                callback();
              },
              trigger: 'change',
            }
          ],
          change: (val) => {
            this.multiple(val);
          },
        },
          {
            type: 'TEXT_AREA',
            label: '备注',
            fieldName: 'remarks',
            placeholder: '请输入备注...',
            rows: 3,
            selfCols: 1 // 元素占据几行
          }

        ];
      }
    }
  };
</script>

<style lang="less" scoped></style>
